#include "GUI.h"
#include "DIALOG.h"
#include "CreateWidget.h"
#include "AppFunction.h"
#include "FileOperation.h"



#include "common_path.h"



//#if defined(DEBUG_IN_PC)
//#include <Windows.h>
//#else
//#endif


#define  bk_color  GUI_MAKE_COLOR(0x292929)
#define  line_color  GUI_MAKE_COLOR(0x00A5FF)


#define COLOR_CONV GUICC_M565



typedef struct PARA PARA;      // para是参数的意思
struct PARA {
	int xSizeWin, ySizeWin;
	int xSizeLCD, ySizeLCD;
	int xSizeBM, ySizeBM;
	int xPosWin, yPosWin;
};

static BUTTON_Handle  hButton_set, hButton_back, hButton_more, hButton_con, hButton_tem, hButton_nav;
static SLIDER_Handle  hSlider;
static GUI_HMEM  hMemBg;
//BitMap hFile1, hFile2, hFile3, hFile4;
static int lastState, lastX, interX, xPosition;
static unsigned char hehe[10] = "SATION";


//extern WM_HWIN CreateWindow2(WM_HWIN H);
//extern void GUI_UC_SetEncodeGBK(void);

WM_HWIN home_widget_create(WM_HWIN hParent);
//WM_HWIN CreateSlideForHome(WM_HWIN hParent);
//WM_HWIN CreateNavigationPage(WM_HWIN hParent);
//void device_widget_create3(WM_HWIN hParent);

void temp_widget_create(WM_HWIN hParent);
void nav3x3_create(WM_HWIN hParent);
void nav3x4_create(WM_HWIN hParent);
void music_widget_create(WM_HWIN hParent);
void temp_widget_create(WM_HWIN hParent);
void timer_widget_create(WM_HWIN hParent);
void setting_widget_create(WM_HWIN hParent);
void curtain_widget_create(WM_HWIN hParent);
//void navigate_widget_create(WM_HWIN hParent);



void cbwin(WM_MESSAGE * pMsg){
	static int16_t stateMachine, initialPosX, orgX, cao = 0;
	WM_MOTION_INFO    * pInfo;
	WM_PID_STATE_CHANGED_INFO     * pState;
	WM_HWIN             childWin;
	PARA              * pPara;
	int                 xPosWinNew;
	int                 yPosWinNew;
	IMAGE_Handle      imageHandle;

    uint32_t size;
	uint8_t *pBuffer;
	int8_t  ret = 0;
	GUI_HMEM hMem;
	GUI_RECT rect;
	int32_t distance;
	int NCode;


    
	childWin = pMsg->hWin;

	WM_GetUserData(childWin, &pPara, sizeof(PARA *));   // 检索通过WM_SetUserData设置的用户数据
	switch (pMsg->MsgId) {
	case WM_CREATE:
		WM_CreateTimer(pMsg->hWin, 1, 1000, 0);
		
		stateMachine = 0;

		break;
	case WM_TIMER:
		WM_RestartTimer(pMsg->Data.v, 1000);
		TimeCallback();
		break;
	case WM_TOUCH:    // 触摸时
//		pState = (WM_PID_STATE_CHANGED_INFO*)pMsg->Data.p;
//		WM_GetWindowRectEx(pMsg->hWin, &rect);

//		switch (stateMachine)
//		{
//		case 0:
//			if (pState->State == 1)
//			{
//				initialPosX = pState->x;
//				orgX        = rect.x0;
//				if ((orgX % 720) != 0)
//					orgX = 0;

//				stateMachine = 1;
//				cao = 1;
//			}

//			break;
//		case 1:
//			distance = pState->x - initialPosX;

//			if (pState->State == 1)
//			{
//				if (distance <= -10 || distance >= 10)
//				{
//					distance = orgX + distance;
//					if (distance > 0)
//						distance = 0;
//					cao = 2;
//					WM_MoveTo(pMsg->hWin, distance, 0);
//				}
//			}
//			else
//			{
//				if (distance <= -100 || distance >= 100)
//				{
//					if (distance <= -100)                          // 左移
//						distance = (rect.x0 - 720) / 720;
//					else                                           // 右移
//						distance = rect.x0 / 720;

//					distance *= 720;
//					cao = 3;
//					WM_MoveTo(pMsg->hWin, distance, 0);
//				}
//				else
//				{
//					if (orgX < -2160 || orgX > 0)
//						orgX = 0;
//					cao = 4;
//					WM_MoveTo(pMsg->hWin, orgX, 0);
//				}

//				stateMachine = 0;
//			}

//			break;
//		default:
//			break;
//		}


		//pState = pMsg->Data.p;
		//if (pState->State == 0)
		//{
		//	WM_GetWindowRectEx(pMsg->hWin, &rect);

		//	distance = rect.x0 - initialPosX;
		//	
		//	if (distance <= -100 || distance >= 100)
		//	{
		//		if (distance <= -100)                          // 左移
		//			distance = (rect.x0 - 720) / 720;
		//		else                                           // 右移
		//			distance = -rect.x0 / 720;

		//		distance *= 720;
		//		WM_MoveTo(pMsg->hWin, distance, 0);
		//	}
		//}

		break;
	case WM_MOTION:  // 运动时
		pInfo = (WM_MOTION_INFO *)pMsg->Data.p;
		if (pInfo) {
			switch (pInfo->Cmd) {
			case WM_MOTION_INIT:
				pInfo->SnapX = 720;
				pInfo->SnapY = 720;
				
				WM_GetWindowRectEx(pMsg->hWin, &rect);
				initialPosX = rect.x0;
				break;
			case WM_MOTION_MOVE:
				SetHomePageMovingFlag(1);
				break;
			}

			//if (pInfo->FinalMove)
			//{
			//	SetHomePageMovingFlag(0);
			//}
		}
		break;
	case WM_PAINT:
		//GUI_SetColor(GUI_MAKE_COLOR(0x292929));
		GUI_Clear();
		WM_GetWindowRect(&rect);
		GUI_MEMDEV_WriteOpaqueAt(hMemBg, 0, 0);

		break;

	}
}


void MainTask(void) {
	static WM_HWIN   hWin;
	PARA            Para = { 0 };
	PARA          * pPara = &Para;  // pPara 是 Para的指针
	uint32_t        file_size;
	int32_t ccll;
	uint16_t  screenSizeX = 720 * 3;    // 一级界面四个页

	DiskInit();

    GUI_Init();


#if !defined(DEBUG_IN_PC)
	/* 触摸支持 */
	app_ts_init();
#endif 
    

	WM_MOTION_Enable(1);   //　允许滑动
	WM_MOTION_SetDefaultPeriod(0);

    WM_MULTIBUF_Enable(1);
    WM_EnableMemdev(WM_HBKWIN);
    
//	WM_SetCreateFlags(WM_CF_MEMDEV);
	
	//GUI_UC_SetEncodeGBK();
	GUI_UC_SetEncodeUTF8();
	GUI_SetFont(&GUI_FontFont32);
    
    GUI_Clear();
    GUI_DispStringAt((char *)hehe, 360, 360);
    
    
	BUTTON_SetDefaultFont(&GUI_FontFont32);
	BUTTON_SetDefaultTextAlign(GUI_TA_HCENTER | GUI_TA_BOTTOM);
	BUTTON_SetDefaultTextColor(GUI_WHITE, BUTTON_CI_UNPRESSED);
	BUTTON_SetDefaultTextColor(GUI_WHITE, BUTTON_CI_PRESSED);

	TEXT_SetDefaultFont(&GUI_FontFont32);
	TEXT_SetDefaultTextColor(GUI_WHITE);
	
	InitAppFunction();
	InitPicture();

	WM_SetSize(WM_HBKWIN, 720, 720);

	hMemBg = GetPicturePathHmem(I_DTA_PATH_BGHOME);

	hWin = WM_CreateWindowAsChild(0, 0, screenSizeX, 720, WM_HBKWIN, WM_CF_SHOW | WM_CF_MOTION_X, cbwin, sizeof(PARA *));

	WM_SetUserData(hWin, &pPara, sizeof(PARA *));



/* 一级界面创建                                                  */
	home_widget_create(hWin);
	nav3x3_create(hWin);
	nav3x4_create(hWin);
	//setting_widget_create(hWin);
	//curtain_widget_create(hWin);
	

/* 二级界面创建                                                  */
	temp_widget_create(WM_HBKWIN);
	music_widget_create(WM_HBKWIN);
	dimmer3c_widget_create(WM_HBKWIN);
	curtain_widget_create(WM_HBKWIN);
	setting_widget_create(WM_HBKWIN);
	timer_widget_create(WM_HBKWIN);
	/*test4x3_widget_create(WM_HBKWIN);
	test4x4_widget_create(WM_HBKWIN);*/
	password_widget_create(WM_HBKWIN);


	while (1){
		GUI_Delay(100);
	}
}